var express = require('express');
var router = express.Router();
var wishedDao=require('../dao/wishesDao');
var userDao=require('../dao/userDao');
var letterDao=require('../dao/letterDao');
var weixinDao=require('../dao/weixinDao');
var svgCaptcha = require('svg-captcha');

var jsonWrite=require('../conf/response');
var client=require('../conf/redis');

client.on("error",function(err){
  console.log("Error",err);
});


// /* GET home page. */
// router.get('/', function(req, res) {
//   res.render('index', { title: 'Express' });
// });
//心愿列表
router.get('/wishes',function(req,res){
    wishedDao.queryWishes(req,res);
});

//心愿认领分类
router.get('/claimwishes',function(req,res){
  wishedDao.queryClaimWishes(req,res);
});

//心愿详情
router.get('/wishdetail',function(req,res){
    wishedDao.queryWishDetail(req,res);
});
//为TA加油
router.post('/like',function(req,res){
    wishedDao.likeWish(req,res);
});
//发送短信验证码
router.post('/sms',function(req,res){
    wishedDao.smsSend(req,res);
});

//图片验证码
router.get('/captcha',function(req,res){
    console.log("in__________________");
  var options={
      size:4,
      noise:3,
      color:true,
      background:'#c8cccc',
      width:108,
      height:40
  };
  var captcha=svgCaptcha.create(options);
  console.log("get captcha",captcha);
  req.session.captcha=captcha.text.toLowerCase();
  console.log("get captcha set",req.sessionID);
  res.set('Content-Type','image/svg+xml');
  res.status(200).send(captcha.data);
});

//验证图片验证码
router.get('/checkcaptcha',function(req,res){
    wishedDao.checkCaptcha(req,res);
});

//获取用户信息
router.get('/userinfo',function(req,res){
    userDao.getUserInfo(req,res);
});

//提交申请
router.post('/claimwish',function(req,res){
    userDao.claimWish(req,res);
});

//假登录：客户端清空用户信息后，输入手机号重新获取用户ID，若用户存在则返回UID，若不存在则创建新用户返回UID
router.post('/login',function(req,res){
    userDao.login(req,res);
});

//获取认领的心愿列表
router.get('/claimedwish',function(req,res){
    userDao.getClaimedWishList(req,res);
});

//获取感谢信列表
router.get('/letters',function(req,res){
    letterDao.getLetterList(req,res);
});

//获取感谢信详情
router.get('/letterdetail',function(req,res){
    letterDao.getLetterDetail(req,res);
});

//微信分享
router.get('/share',function(req,res){
  weixinDao.getSignature(req,res);
});

router.get('/clear',function(req,res){
  client.del("access_token");
  client.del("jsapi_ticket");
  jsonWrite(res,{"status":0,"msg":"del_SUCCESS"});
});
module.exports = router;
